package com.beiding.jsonanalyzer.querylink;

import com.beiding.jsonanalyzer.flow.ValueNode;
import com.beiding.jsonanalyzer.flow.BranchNode;
import com.beiding.jsonanalyzer.visit.VisitAble;
import com.beiding.jsonanalyzer.visit.VisitorAdapter;

public abstract class ComputeNode<W extends ValueNode> extends QueryLinkNodeAdapter<W> {

    public ComputeNode(QueryLinkNode<W> pre) {
        super(pre);
    }

    @Override
    protected VisitAble<BranchNode<W>> doExec(VisitAble<BranchNode<W>> preResult) {
        return nextBranchAccepter -> {

            preResult.visit(new VisitorAdapter<BranchNode<W>>(nextBranchAccepter) {
                @Override
                public void onNode(BranchNode<W> b) {


                    //复制一个流程节点
                    BranchNode<W> queuedBranch = new BranchNode<>(b.getBranch());

                    //将两个流程节点前后绑定 且
                    bindByComputeNode(b, queuedBranch);
                    nextBranchAccepter.onNode(queuedBranch);

                }
            });
        };
    }

    /**
     * 构建算子以绑定两个分支节点
     *
     * @param preResult
     * @param newBranchNode
     */
    protected abstract void bindByComputeNode(BranchNode<W> preResult, BranchNode<W> newBranchNode);

}
